/**
 * Copyright &copy; com.smooth.common
 */
package com.smooth.site.sx.mag.order.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.common.site.common.persistence.Page;
import com.smooth.common.site.common.utils.IdGenerater;
import com.smooth.dao.gen.BizGoodsMapper;
import com.smooth.dao.gen.BizOrderMapper;
import com.smooth.dao.gen.bean.BaseAreas;
import com.smooth.dao.gen.bean.BaseCities;
import com.smooth.dao.gen.bean.BizAddress;
import com.smooth.dao.gen.bean.BizGoods;
import com.smooth.dao.gen.bean.BizOrder;
import com.smooth.dao.gen.bean.BizOrderExample;
import com.smooth.site.sx.mag.address.service.BizAddressService;
import com.smooth.site.sx.mag.basedata.area.service.BaseAreasService;
import com.smooth.site.sx.mag.basedata.city.service.BaseCitiesService;
import com.smooth.site.sx.mag.goods.enums.GoodsStatus;

/**
 * 订单Service
 * @author bm
 * @version 2019-02-07
 */
@Service
@Transactional(readOnly = true)
public class BizOrderService{
	Logger log = LoggerFactory.getLogger(BizOrderService.class);
	@Autowired
	BizOrderMapper bizOrderMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BizOrder get(String no) {
		return bizOrderMapper.selectByPrimaryKey(no);
	}
	
	public List<BizOrder> findList(BizOrder bizOrder) {
		BizOrderExample bizOrderExample = new BizOrderExample();
		BizOrderExample.Criteria criteria = bizOrderExample.createCriteria();
		
		return bizOrderMapper.selectByExample(bizOrderExample);
	}
	
	public Page<BizOrder> findPage(Page<BizOrder> page, BizOrder bizOrder) {
		BizOrderExample bizOrderExample = new BizOrderExample();
		BizOrderExample.Criteria criteria = bizOrderExample.createCriteria();
		
		bizOrderExample.setPage(page);
		
		List<BizOrder> dlist = bizOrderMapper.selectByExample(bizOrderExample);
		page.setList(dlist);
		page.setCount(bizOrderMapper.countByExample(bizOrderExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public BizOrder save(BizOrder bizOrder) {
		
		setAddressInfo(bizOrder);
		if(StringUtils.isNotEmpty(bizOrder.getId())&&get(bizOrder.getId())!=null) {
			int optNum = bizOrderMapper.updateByPrimaryKeySelective(bizOrder);
			if(optNum==1) {
				log.info("更新 student 数据【"+bizOrder.toString()+"】成功");
			}else {
				log.error("更新 student 数据【"+bizOrder.toString()+"】失败");
			}
			return bizOrderMapper.selectByPrimaryKey(bizOrder.getId());
		}else {
			setGoodsInfo(bizOrder);
			Date date = new Date();
			bizOrder.setCreateDate(date);
			bizOrder.setId(idGenerater.genClsNo("bizOrder"));
			bizOrder.setUpdateDate(date);
			int optNum =  bizOrderMapper.insertSelective(bizOrder);
			if(optNum==1) {
				log.info("插入 student 数据【"+bizOrder.toString()+"】成功");
			}else {
				log.error("插入 student 数据【"+bizOrder.toString()+"】失败");
			}
			return bizOrder;
		}
	}
	@Autowired
	BaseAreasService baseAreasService;
	@Autowired
	BaseCitiesService baseCitiesService;
	@Autowired
	BizGoodsMapper bizGoodsMapper;
	private void setGoodsInfo(BizOrder bizOrder) {
		String goodNo = bizOrder.getGoodId();
		BizGoods bizGoods = bizGoodsMapper.selectByPrimaryKey(goodNo);
		
		GoodsStatus goodsStatus = GoodsStatus.getStatus(bizGoods.getGoodStatus());
		if(!GoodsStatus.ONLINE.equals(goodsStatus)) {
			throw new SiteException(SiteCodeEnums.param_error,goodNo+"该商品已下架，请联系管理员");
		}
		
		bizOrder.setGoodEndTime(bizGoods.getEndTime());
		bizOrder.setGoodDesc(bizGoods.getGoodDesc());
		bizOrder.setGoodId(bizGoods.getId());
		bizOrder.setGoodName(bizGoods.getGoodTitle());
		bizOrder.setGoodStartTime(bizGoods.getStartTime());
		bizOrder.setGoodPic(bizGoods.getGoodPic());
		bizOrder.setGoodPics(bizGoods.getGoodPics());
	}
	@Autowired
	BizAddressService bizAddressService;
	private void setAddressInfo(BizOrder bizOrder) {
		
		if(StringUtils.isNotBlank(bizOrder.getAddressId())) {
			BizAddress bizAddress = bizAddressService.get(bizOrder.getAddressId());
			bizOrder.setAddressDetail(bizAddress.getAddressDetail());
			bizOrder.setAreaId(bizAddress.getAreaId());
			bizOrder.setAreaName(bizAddress.getAreaName());
			bizOrder.setCityId(bizAddress.getCityId());
			bizOrder.setCityName(bizAddress.getCityName());
			bizOrder.setProvinceId(bizAddress.getProvinceId());
			bizOrder.setProvinceName(bizAddress.getProvinceName());
			bizOrder.setShouhuoName(bizAddress.getUserName());
			bizOrder.setShouhuoTelno(bizAddress.getUserTelno());
		}
//		if(StringUtils.isNotBlank(bizOrder.getAreaId())) {
//			BaseAreas baseAreas = baseAreasService.get(bizOrder.getAreaId());
//			
//			bizOrder.setCityId(baseAreas.getCityId());
//			bizOrder.setCityName(baseAreas.getCityId());
//			bizOrder.setAreaName(baseAreas.getArea());
//			
//		}
//		if(StringUtils.isNotBlank(bizOrder.getCityId())) {
//			BaseCities baseCities = baseCitiesService.get(bizOrder.getCityId());
//			if(baseCities!=null) {
//				bizOrder.setCityId(baseCities.getId());
//				bizOrder.setCityName(baseCities.getCityName());
//				
//				bizOrder.setProvinceId(baseCities.getProvinceId());
//				bizOrder.setProvinceName(baseCities.getProvinceName());
//			}
//		}
		
	}
	@Transactional(readOnly = false)
	public void delete(BizOrder bizOrder) {
		bizOrderMapper.deleteByPrimaryKey(bizOrder.getId());
	}
	
}